<?php
namespace App\Admin\Controllers\SysManage;

use App\Models\Amazon\AmazonOrderCondition;
use App\Models\Basics\AmazonAccount;
use App\Models\Basics\SysPlatAccount;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

class OrderApiController extends AdminController{

    protected $title = '订单API';

    protected function grid()
    {
        $grid = new Grid(new AmazonOrderCondition());
        $grid->model()->collection(function (Collection $collection){
            foreach($collection as $index => $item) {
                $item->account_ = $item->account_id.'_'.($item->account->account ??  'unknown').'-'
                    .(isset($item->account->api)&&array_key_exists($item->account->api,AmazonAccount::$_api) ? AmazonAccount::$_api[$item->account->api] : 'unknown');
            }
            return $collection;
        });
        $grid->column('id','ID')->style('width: 5%');
        $grid->column('account_','账号');
        $grid->column('next_start_time','订单抓取开始时间')->datetime()->sortable();

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
        });
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
            $create->select('account_id', '账号')->options(SysPlatAccount::options());
            $create->datetime('next_start_time', '订单抓取开始时间');
        });
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->equal('account_id', '账号')->select(SysPlatAccount::options());
            });
        });
        $grid->disableRowSelector();
        $grid->disableExport();
        return $grid;
    }

    public function form()
    {
        $form = new Form(new AmazonOrderCondition());

        $form->hidden('id');
        $form->hidden('created_at');
        $form->hidden('updated_at');

        $form->select('account_id', '账号')->options(SysPlatAccount::options())
            ->creationRules(['required', "unique:mysql_amazon.amazon_order_condition"])
            ->updateRules(['required', "unique:mysql_amazon.amazon_order_condition,account_id,{{id}}"]);
        $form->datetime('next_start_time', '订单抓取开始时间')->help('时区:UTC/GMT 0 (零时区)');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });
        return $form;
    }

    public function destroy($id)
    {
        try{
            DB::beginTransaction();
            $aoc = AmazonOrderCondition::query()->find($id);
            $count = SysPlatAccount::query()->where('id',$aoc->account_id)->where('api',1)->count();
            if($count > 0){
                return response()->json(['status' => false, 'message' => 'API启用中勿删除']);
            }
            $aoc->delete();
            DB::commit();
            return response()->json(['status' => true, 'message' => '操作成功！']);
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统错误！']);
        }
    }
}
